﻿
namespace KinonekoSoftware.Studio.Pages.Theories.Rarities
{
#if AVALONIA


    using Color = Avalonia.Media.Color;
    using Avalonia.Media.Imaging;
    using KinonekoSoftware.CodariaDB.Documents.Theories.Rarities;
    using KinonekoSoftware.CodariaDB.Utils;
    
    [Operation(ResourceType.Icon)]
    public partial class NewRarityViewModel
    {
        private ImageEditFeedback _imageIEF;
        private ImageEditFeedback _iconIEF;
        private Bitmap            _icon;
        private Bitmap            _image;
        private Color             _foregroundColor;
        private Color             _backgroundColor;
        private Color             _strokeColor;


        private static void GetBitmap(string value, Action<Bitmap> callback)
        {

            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            if (!Platform.Cache.TryGetValue(value, out var wIMG))
            {
                return;
            }

            if (!wIMG.TryGetTarget(out var img) && img is Bitmap)
            {
                return;
            }
            
            callback?.Invoke((Bitmap)img);
        }

        private async void DoSelectIconCommand()
        {
            var r = await Platform.Icon();

            if (!r.IsFinished)
            {
                if (!string.IsNullOrEmpty(r.Reason))
                {
                    await Danger(TitleSR.DangerOperation, r.Reason);
                }
                return;
            }

            _iconIEF = r.Value;
            Icon     = _iconIEF.Image;
        }

        private async void DoSelectImageCommand()
        {
            var r = await Platform.Icon();

            if (!r.IsFinished)
            {
                if (!string.IsNullOrEmpty(r.Reason))
                {
                    await Danger(TitleSR.DangerOperation, r.Reason);
                }

                return;
            }

            _imageIEF = r.Value;
            Image     = _imageIEF.Image;
        }

        private void DoResetIconCommand()
        {
            Icon     = null;
            _iconIEF = null;

            if (_element is not null)
            {
                _element.Icon = null;
            }
        }

        private void DoResetImageCommand()
        {
            Image     = null;
            _imageIEF = null;

            if (_element is not null)
            {
                _element.Image = null;
            }
        }

        protected override async Task<Rarity> OnFinish(bool editing)
        {
            var iconFS = Platform.GetEngine<IconSystem>();
            if (editing)
            {

                if (_iconIEF is not null)
                {
                    var oldIcon = _element.Icon;
                    var newIcon = _iconIEF.ImageID;
                    
                    _element.Icon = newIcon;
                    
                    //
                    // 写入
                    await iconFS.WriteAsync(newIcon, _iconIEF.ImageBuffer);
                    
                    //
                    // 移除占用
                    iconFS.RemoveOccupation(oldIcon);
                    iconFS.InsertOccupation(newIcon);
                }

                if (_imageIEF is not null)
                {
                    var oldImage = _element.Image;
                    var newImage = _imageIEF.ImageID;
                    
                    _element.Image = newImage;
                    
                    
                    await iconFS.WriteAsync(newImage, _imageIEF.ImageBuffer);
                    
                    
                    //
                    // 移除占用
                    iconFS.RemoveOccupation(oldImage);
                    iconFS.InsertOccupation(newImage);
                }

                _element.Background = _background;
                _element.Foreground = _foreground;
                _element.Stroke     = _stroke;
                _element.Name       = _name;
                _element.Intro      = _intro;
                _element.Level      = _level;
            }
            else
            {
                if (_iconIEF is not null)
                {
                    var newIcon = _iconIEF.ImageID;
                    
                    //
                    // 写入
                    await iconFS.WriteAsync(newIcon, _iconIEF.ImageBuffer);
                    
                    
                    iconFS.InsertOccupation(newIcon);
                }

                if (_imageIEF is not null)
                {
                    var newImage = _imageIEF.ImageID;
                    
                    await iconFS.WriteAsync(newImage, _imageIEF.ImageBuffer);
                    
                    iconFS.InsertOccupation(newImage);
                }

                _element = new Rarity
                {
                    Id         = ID.Get(),
                    Background = _background,
                    Foreground = _foreground,
                    Icon       = _iconIEF?.ImageID,
                    Image      = _imageIEF?.ImageID,
                    Stroke     = _stroke,
                    Name       = _name,
                    Intro      = _intro,
                    Level      = _level,
                };
            }

            return _element;
        }


        public Color BackgroundColor
        {
            get => _backgroundColor;
            set
            {
                SetValue(ref _backgroundColor, value);
                Background = value.ToString();
            }
        }

        public Color ForegroundColor
        {
            get => _foregroundColor;
            set
            {
                SetValue(ref _foregroundColor, value);
                Foreground = value.ToString();
            }
        }

        public Color StrokeColor
        {
            get => _strokeColor;
            set
            {
                SetValue(ref _strokeColor, value);
                Stroke = value.ToString();
            }
        }

        public Bitmap Image
        {
            get => _image;
            set => SetValue(ref _image, value);
        }

        public Bitmap Icon
        {
            get => _icon;
            set => SetValue(ref _icon, value);
        }
    }
#endif
}